package com.sbio.udiapp.cpcl;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson2.util.DateUtils;
import com.blankj.utilcode.util.StringUtils;
import com.sbio.udiapp.BuildConfig;
import com.sbio.udiapp.common.bean.PrintVo;
import com.sbio.udiapp.di.ActivityManager;
import com.sbio.udiapp.ui.model.RawMaterialListItemView;
import com.sbio.udiapp.util.DefaultSharedPreferencesUtil;
import com.sbio.udiapp.util.StringUtil;
import com.sbio.udiapp.util.ToastUtil;

import java.util.Optional;

import cpcl.PrinterHelper;

public class PrintUtil {

    public static void printQR(PrintVo printVo) {
        Context context = ActivityManager.getCurrentActivity();
        if (DefaultSharedPreferencesUtil.useDeptIsProduct(context)) {
            printQR4Product(printVo);
        } else {
            printQR4Warehouse(printVo);
        }
    }

    /**
     * 为生产打印二维码
     *
     * @param printVo
     */
    public static void printQR4Product(PrintVo printVo) {
        Context context = ActivityManager.getCurrentActivity();

        if (!checkClick.isClickEvent()) return;

        if (!PrinterHelper.IsOpened()) {
            ToastUtil.show("请连接打印机");
            return;
        }
        try {
            int barcodeX = 10;
            int barcodeY = 20;
            //二维码单位
            int barcodeU = 10;
            //二维码一行有多少个点
            int barcodeDots = 21;
            //二维码边长
            int barcodeSide = barcodeU * barcodeDots;
            //二维码和文字之间的间隔
            int barcodeTextSpace = 30;
            //设置字体
            String textFont = "2";
            //文字高度
            int textHeight = 24;

            PrinterHelper.printAreaSize("0", "600", "400", "400", "1");
            PrinterHelper.PrintQR(PrinterHelper.BARCODE, barcodeX + "", barcodeY + "", "2", barcodeU + "",
                    printVo.getPackNo());
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", barcodeY + barcodeSide + barcodeTextSpace + "", "注册证号: " + printVo.getRegistrationCertificateNo());

            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "280", "货号: " + Optional.ofNullable(printVo.getArticleNo()).orElse(""));
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "320", "注册证号: " + printVo.getRegistrationCertificateNo());
            int textX = barcodeX + barcodeSide + barcodeTextSpace;
            barcodeY = -10;
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "代码: " + printVo.getMaterialNo());
            barcodeY += textHeight * 2.5;

            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "名称: " + printVo.getMaterialName());
//            PrinterHelper.AutLine2(textX + "", barcodeY + "",34*8+"",8, false, false, "名称: " + printVo.getMaterialName());
//            PrinterHelper.AutLine(textX + "", barcodeY + "", 34 * 8, 8, false, false, "名称: "+printVo.getMaterialName());
            barcodeY += textHeight * 4.5;

            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "规格: " + printVo.getSpec());
            barcodeY += textHeight * 2.5;
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "批号: " + printVo.getLot());
//            PrinterHelper.AutLine2(textX + "", barcodeY + "",34*8+"",8, false, false, "批号: " + printVo.getLot());
//            PrinterHelper.AutLine(textX + "", barcodeY + "",34*8,8, false, false, "批号: " + printVo.getLot());

            barcodeY += textHeight * 1.5;
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "数量: " + printVo.getCount());
//            barcodeY += textHeight * 1.5;
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "货号: " + Optional.ofNullable(printVo.getArticleNo()).orElse(""));

            PrinterHelper.Form();
            PrinterHelper.Print();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e("HPRTSDKSample", (new StringBuilder("Activity_Main --> onClickWIFI ")).append(e.getMessage()).toString());
        }

    }

    /**
     * 为仓库打印二维码
     *
     * @param printVo
     */
    public static void printQR4Warehouse(PrintVo printVo) {
        Context context = ActivityManager.getCurrentActivity();

        if (!checkClick.isClickEvent()) return;

        if (!PrinterHelper.IsOpened()) {
            ToastUtil.show("请连接打印机");
            return;
        }
        try {
            int barcodeX = 10;
            int barcodeY = 60;
            //二维码单位
            int barcodeU = 10;
            //二维码一行有多少个点
            int barcodeDots = 21;
            //二维码边长
            int barcodeSide = barcodeU * barcodeDots;
            //二维码和文字之间的间隔
            int barcodeTextSpace = 30;
            //设置字体
            String textFont = "2";
            //文字高度
            int textHeight = 24;

            PrinterHelper.printAreaSize("0", "600", "400", "400", "1");
            PrinterHelper.PrintQR(PrinterHelper.BARCODE, barcodeX + "", barcodeY + "", "2", barcodeU + "",
                    printVo.getPackNo());
            int textX = barcodeX + barcodeSide + barcodeTextSpace;
            barcodeY = 40;
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "箱码: " + printVo.getPackNo());
            PrinterHelper.Form();
            PrinterHelper.Print();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e("HPRTSDKSample", (new StringBuilder("Activity_Main --> onClickWIFI ")).append(e.getMessage()).toString());
        }

    }

    /**
     * 蓝牙是否打开
     *
     * @return
     */
    public static boolean isOpened() {
        return PrinterHelper.IsOpened();
    }

    /**
     * 检查打印机是否准备好了
     * 如果准备好了，就先打印
     *
     * @return
     */
    public static boolean checkPrintPrepared() {
        if (BuildConfig.DEBUG) {
            return true;
        }
        if (!PrinterHelper.IsOpened()) {
            ToastUtil.show("请连接打印机");
            return false;
        }
        return true;
    }


    /**
     * 为原材料打印二维码
     *
     * @param rawMaterialListItemView
     */
    public static void printRawMaterial(RawMaterialListItemView rawMaterialListItemView) {
        Context context = ActivityManager.getCurrentActivity();

        if (!checkClick.isClickEvent()) return;

        if (!PrinterHelper.IsOpened()) {
            ToastUtil.show("请连接打印机");
            return;
        }
        try {
            int barcodeX = 10;
            int barcodeY = 20;
            //二维码单位
            int barcodeU = 5;
            //二维码一行有多少个点
            int barcodeDots = 21;
            //二维码边长
            int barcodeSide = barcodeU * barcodeDots;
            //二维码和文字之间的间隔
            int barcodeTextSpace = 10;
            //设置字体
            String textFont = "2";
            //文字高度
            int textHeight = 24;
            int textX = barcodeX + barcodeSide + barcodeTextSpace;

            String expiryDate = StringUtils.null2Length0(DateUtils.format(rawMaterialListItemView.getExpiryDate(), "yyyy-MM-dd"));

            String horizontal = "400";
            PrinterHelper.printAreaSize("0", horizontal, "400", "400", "1");
            PrinterHelper.PrintQR(PrinterHelper.BARCODE, barcodeX + "", barcodeY + "", "2", barcodeU + "",
                    rawMaterialListItemView.getInnerBarcode());
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", barcodeY + barcodeSide + barcodeTextSpace + "", "注册证号: " + rawMaterialListItemView.getRegistrationCertificateNo());
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "240", "" + StringUtils.null2Length0(rawMaterialListItemView.getInnerBarcode()));

            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", "30", "数量: " + rawMaterialListItemView.getQty());
            String temperature = rawMaterialListItemView.getTemperature();
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", "60", "温度: " + (StringUtils.isTrimEmpty(temperature) ? "常温" : temperature));
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", "90", "成品批号: " + StringUtils.null2Length0(rawMaterialListItemView.getParentMaterialLot()));
            barcodeY = -10;
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "物料编码: " + StringUtils.null2Length0(rawMaterialListItemView.getMaterialNo()));
//            barcodeY += textHeight * 2.5;
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "130", "成品名称: " + rawMaterialListItemView.getParentMaterialName());
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "190", "成品规格: " + rawMaterialListItemView.getParentMaterialSpec());
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "220", "成品货号: " + rawMaterialListItemView.getParentMaterialItemNo());

            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "250", "物料名称: " + StringUtils.null2Length0(rawMaterialListItemView.getMaterialName()));
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "280", "规格型号: " + StringUtils.null2Length0(rawMaterialListItemView.getSpec()));
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "310", "批号: " + StringUtils.null2Length0(rawMaterialListItemView.getLot()));
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "340", "有效期: " + expiryDate);
//            barcodeY += textHeight * 1.5;
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "货号: " + Optional.ofNullable(rawMaterialListItemView.getArticleNo()).orElse(""));

            PrinterHelper.Form();
            PrinterHelper.Print();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e("HPRTSDKSample", (new StringBuilder("Activity_Main --> onClickWIFI ")).append(e.getMessage()).toString());
        }

    }

    /**
     * 为收料单打印原材料二维码
     *
     * @param rawMaterialListItemView
     */
    public static void printRawMaterial4ReceiveBill(RawMaterialListItemView rawMaterialListItemView) {
        Context context = ActivityManager.getCurrentActivity();

        if (!checkClick.isClickEvent()) return;

        if (!PrinterHelper.IsOpened()) {
            ToastUtil.show("请连接打印机");
            return;
        }
        try {
            int barcodeX = 10;
            int barcodeY = 20;
            //二维码单位
            int barcodeU = 5;
            //二维码一行有多少个点
            int barcodeDots = 21;
            //二维码边长
            int barcodeSide = barcodeU * barcodeDots;
            //二维码和文字之间的间隔
            int barcodeTextSpace = 10;
            //设置字体
            String textFont = "2";
            //文字高度
            int textHeight = 24;
            int textX = barcodeX + barcodeSide + barcodeTextSpace;

            String expiryDate = StringUtils.null2Length0(DateUtils.format(rawMaterialListItemView.getExpiryDate(), "yyyy-MM-dd"));
            String productDate = StringUtils.null2Length0(DateUtils.format(rawMaterialListItemView.getProductDate(), "yyyy-MM-dd"));

            String horizontal = "400";
            PrinterHelper.printAreaSize("0", horizontal, "600", "400", "1");
            PrinterHelper.PrintQR(PrinterHelper.BARCODE, barcodeX +440+ "", barcodeY + "", "2", barcodeU + "",
                    rawMaterialListItemView.getInnerBarcode());
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", barcodeY + barcodeSide + barcodeTextSpace + "", "注册证号: " + rawMaterialListItemView.getRegistrationCertificateNo());
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "240", "" + StringUtils.null2Length0(rawMaterialListItemView.getInnerBarcode()));

            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", String.valueOf(barcodeX), "30", "物料编码: " + rawMaterialListItemView.getMaterialNo());
            String temperature = rawMaterialListItemView.getTemperature();
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", String.valueOf(barcodeX), "60", "批号: " + StringUtils.null2Length0(rawMaterialListItemView.getLot()));
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", "", "90", "开瓶效期: ");
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", "90", "批号: " + StringUtils.null2Length0(rawMaterialListItemView.getLot()));
            barcodeY = -10;
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "物料编码: " + StringUtils.null2Length0(rawMaterialListItemView.getMaterialNo()));
//            barcodeY += textHeight * 2.5;
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "130", "物料名称: " + StringUtils.null2Length0(rawMaterialListItemView.getMaterialName()));
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "190", "规格型号/存储条件: " + StringUtils.null2Length0(rawMaterialListItemView.getSpec()) +"/"+ (StringUtils.isTrimEmpty(temperature) ? "常温" : temperature));
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "220", "成品货号: " + rawMaterialListItemView.getParentMaterialItemNo());

            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "220", "到库日期: ");
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "250", "生产日期: " + productDate);
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "280", "有效期至: " + expiryDate);
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "310", "开瓶日期:           开瓶效期：");
            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", barcodeX + "", "340", "供应商: " + StringUtils.null2Length0(rawMaterialListItemView.getSupplier()));
//            barcodeY += textHeight * 1.5;
//            PrinterHelper.Text(PrinterHelper.TEXT, textFont, "0", textX + "", barcodeY + "", "货号: " + Optional.ofNullable(rawMaterialListItemView.getArticleNo()).orElse(""));

            PrinterHelper.Form();
            PrinterHelper.Print();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e("HPRTSDKSample", (new StringBuilder("Activity_Main --> onClickWIFI ")).append(e.getMessage()).toString());
        }

    }


}
